package edu.uwosh.cs342.group1;

import java.util.ArrayList;

import edu.uwosh.cs342.group1.database.DbCloudHelper;
import edu.uwosh.cs342.group1.storage.Question;
import edu.uwosh.cs342.group1.storage.Quiz;
import edu.uwosh.cs342.group1.storage.Scores;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class ShortAnswerDisplay extends Activity {
	TextView question;
	ArrayList<Question> questions;
	int correct;
	int total;
	Question currentQuestion;
	EditText answerBox;
	DbCloudHelper db;
	int quizID;
	String quizName, username;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		db = new DbCloudHelper();
		
		setContentView(R.layout.short_answer);
		correct = this.getIntent().getIntExtra("correct", 0);
		total = this.getIntent().getIntExtra("total",0);
		questions = this.getIntent().getParcelableArrayListExtra("questions");
		quizID = this.getIntent().getIntExtra("QuizId",0);
		quizName = this.getIntent().getStringExtra("quizName");
		username = this.getIntent().getStringExtra("username");
		
		currentQuestion = questions.get(total);
		
		question = (TextView)findViewById(R.id.question);
		answerBox = (EditText)findViewById(R.id.answer);
		
		
		question.setText("Question " + (1+total)+ " of " + questions.size() + ": " +currentQuestion.getTitle());

		
	}
	
	public void submit(View v) {
		String answer = answerBox.getText().toString();
		if(answer.compareTo(currentQuestion.getKey())==0) {
			correct++;
		}
		total++;
		if(questions.size()>total) {
			Question q = questions.get(total);
			if(q.isMultipleChoice()) {
				Intent i = new Intent(ShortAnswerDisplay.this,
						MultipleChoiceDisplay.class);
				i.putExtra("questions", questions);
				i.putExtra("correct", correct);
				i.putExtra("total", total);
				i.putExtra("quizName", quizName);
				i.putExtra("QuizId",quizID);
				i.putExtra("username",username);
				ShortAnswerDisplay.this.startActivity(i);
				finish();
				System.out.println("MultipleChoice next!");
			}else if(q.isTrueFalse()){
				Intent i = new Intent(ShortAnswerDisplay.this,
						TrueFalseDisplay.class);
				i.putExtra("questions", questions);
				i.putExtra("correct", correct);
				i.putExtra("total", total);
				i.putExtra("quizName", quizName);
				i.putExtra("QuizId",quizID);
				i.putExtra("username",username);
				ShortAnswerDisplay.this.startActivity(i);
				finish();
				System.out.println("TrueFalse next!");
			} else if(q.isShortAnswer()) {
				Intent i = new Intent(ShortAnswerDisplay.this,
						ShortAnswerDisplay.class);
				i.putExtra("questions", questions);
				i.putExtra("correct", correct);
				i.putExtra("total", total);
				i.putExtra("quizName", quizName);
				i.putExtra("QuizId",quizID);
				i.putExtra("username",username);
				ShortAnswerDisplay.this.startActivity(i);
				finish();
				System.out.println("ShortAnswer next!");
			}
			
		}else {
			AlertDialog.Builder alert = new AlertDialog.Builder(this);
			
			alert.setTitle("Submit Score?");
			alert.setMessage("Would you like to submit your score?\nCorrect: " +
			correct + "\nTotal: "+total);
			
			alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int whichButton) {
					// TODO Auto-generated method stub
					Scores score = new Scores();
					Quiz quiz = new Quiz();
					quiz.setId(quizID);
					quiz.setName(quizName);
					score.setUsername(username);
					score.setQuiz(quiz);
					score.setScore(Scores.calculateScore(correct, total));
					score.setIsPublic(1);
					db.SubmitScore(score);
					Intent i = new Intent(ShortAnswerDisplay.this,QuizAppActivity.class);
					//startActivity(i);
					finishActivity(1);
					finish();
				}
			});
			
			alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Scores score = new Scores();
					Quiz quiz = new Quiz();
					quiz.setId(quizID);
					quiz.setName(quizName);
					score.setUsername(username);
					score.setQuiz(quiz);
					score.setScore(Scores.calculateScore(correct, total));
					score.setIsPublic(0);
					db.SubmitScore(score);
					Intent i = new Intent(ShortAnswerDisplay.this,QuizAppActivity.class);
					//startActivity(i);
					finishActivity(1);
					finish();
				}
			});
			
			alert.show();
		}
	}
	
//	public void onPause() {
//		finish();
//	}
}
